package com.szgr.baoyin.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;
import com.szgr.baoyin.MyApplication;
import com.szgr.baoyin.R;
import com.szgr.baoyin.model.MarketInfo;
import com.szgr.baoyin.utils.Constant;
import com.szgr.baoyin.utils.MyTool;
import com.szgr.baoyin.utils.RLog;
import com.szgr.baoyin.utils.ToastUtils;
import com.szgr.baoyin.utils.ViewFindUtils;
import com.szgr.baoyin.zxing.MipcaActivityCapture;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 * 经办人
 */
public class FinanceOperatorActivity extends BaseActivity {
    private Context mContext;
    private Handler mHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case 23:
                    String error = (String)msg.obj;
                    ToastUtils.showToast(error);
                    break;
                case 45:
                    getPerson();
                    break;
                case 24:
                    tv_name.setText(marketInfo.getManager());
                    String sex = marketInfo.getSex();
                    if("1".equals(sex)){
                        tv_sex.setText("男");
                    }else if("2".equals(sex)){
                        tv_sex.setText("女");
                    }else{
                        tv_sex.setText("其他");
                    }
                    tv_phone.setText(marketInfo.getPhone());
                    tv_email.setText(marketInfo.getEmail());
                    MyTool.showPicture(mContext,img_head,marketInfo.getAvatar());
                    break;
            }
        }
    };
    private MarketInfo marketInfo;
    private ImageView img_head;
    private TextView tv_name;
    private TextView tv_sex;
    private TextView tv_phone;
    private TextView tv_email;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_finance_operator);
        super.onCreate(savedInstanceState);
    }

    @Override
    public void initActionBar() {
        actionBarTitle.setText("商务代表");
        actionBarConfirm.setVisibility(View.VISIBLE);
        actionBarConfirm.setText("二维码");
    }

    @Override
    public void initView() {
        mContext = this;
        img_head = ViewFindUtils.find(view, R.id.operator_head_view);
        tv_name = ViewFindUtils.find(view, R.id.operator_name);
        tv_sex = ViewFindUtils.find(view, R.id.operator_sex);
        tv_phone = ViewFindUtils.find(view, R.id.operator_phone);
        tv_email = ViewFindUtils.find(view, R.id.operator_email);

        getPerson();
    }

    private void getPerson() {
        StringRequest sr = new StringRequest(Request.Method.POST, Constant.URL_REPRE,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String s) {
                        RLog.s("---result1--->" + s);
                        try {
                            JSONObject jsonObject = new JSONObject(s);
                            JSONObject datas = jsonObject.getJSONObject("datas");
                            if(datas.has("error")){
                                String error = datas.getString("error");
                                mHandler.obtainMessage(23,error).sendToTarget();
                            }else{
                                JSONObject market_info = datas.getJSONObject("market_info");
                                marketInfo = new Gson().fromJson(market_info.toString(), MarketInfo.class);
                                mHandler.obtainMessage(24).sendToTarget();
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {

            }
        }){
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String,String> map = new HashMap<>();
                map.put("key",MyApplication.getmInstance().getKey());
                return map;
            }
        };
        MyApplication.getmInstance().addToRequestQueue(sr);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.action_bar_left:
                finish();
                break;
            case R.id.action_bar_confirm:
                Intent intent = new Intent();
                intent.setClass(this, MipcaActivityCapture.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivityForResult(intent, 1);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case 1:
                if(resultCode == RESULT_OK){
                    Bundle bundle = data.getExtras();
                    //显示扫描到的内容

                    //显示

                    subRepresent(bundle.getString("result"));
                }
                break;
        }
    }

    private void subRepresent(final String code) {
        StringRequest sr = new StringRequest(Request.Method.POST, Constant.URL_SUBMIT_REPRE,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String s) {
                        RLog.s("---result2--->" + s);
                        try {
                            JSONObject jsonObject = new JSONObject(s);
                            JSONObject datas = jsonObject.getJSONObject("datas");
                            if(datas.has("result")){
                                String result = datas.getString("result");
                                if("1".equals(result)){
                                    mHandler.obtainMessage(45).sendToTarget();
                                }
                            }

                            if(datas.has("error")){
                                String error = datas.getString("error");
                                mHandler.obtainMessage(23,error).sendToTarget();
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {

            }
        }){
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String,String> map = new HashMap<>();
                map.put("key",MyApplication.getmInstance().getKey());
                map.put("qrcode",code);
                map.put("client","android");
                return map;
            }
        };
        MyApplication.getmInstance().addToRequestQueue(sr);
    }
}
